import requests
from lxml import etree
import csv
import time

# 发送请求 获取页面源代码
url = "https://www.zbj.com/fw/?k=saas"
head = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
}

resp = requests.get(url, headers=head)

# 解析数据
html = etree.HTML(resp.text)
all_box = html.xpath('//*[@id="__layout"]/div/div[3]/div[1]/div[4]/div/div[2]/div/div[2]/div')

# 存入文件中
f = open("pig_data.csv", mode="w", encoding="utf-8")
csv_writer = csv.writer(f)

# 提取单个数据
for box in all_box:
    # 价格
    price = box.xpath('./div/div[3]/div[1]/span/text()')[0].strip("¥")
    # 标题
    all_title = box.xpath('./div/div[3]/div[2]/a/span//text()')
    # 连接标题
    title = ''
    for t in all_title:
        title += t
    # 公司名
    con_name = box.xpath('./div/div[5]/div/div/div/text()')[0]
    csv_writer.writerow([price, title, con_name])
    time.sleep(3)


f.close()
resp.close()
print("over")
